import torch
import matplotlib.pyplot as plt

"""
梯度消失，在e与w的图像像处于斜率为0的状态，那么体现在训练过程中，
则表示梯度将不会进一步下降了
"""

x = torch.linspace(8, 10, 20)
y = torch.sin(x)

# 设置w与b进行训练
w = torch.tensor(0.1, requires_grad=True)
b = torch.tensor(0.2, requires_grad=True)


def tanh(x):
    a = 1 - torch.exp(-x)
    b = 1 + torch.exp(-x)
    return a / b


epochs = 100
for epoch in range(epochs):
    predict_y = tanh(w * x) + b
    loss = torch.mean((predict_y - y) ** 2)
    print(loss)
    loss.backward()
    with torch.no_grad():
        w -= w.grad * 0.01
        b -= b.grad * 0.01
        w.grad.zero_()
        b.grad.zero_()

# plt.plot(x, y, 'r-')
# plt.show()
